var $part = $('input[name="part"]');
var $btn = $("#btn_save");
$(function(){
	zoo();
})
function zoo(){
	$.ajax({
        type: "post",
        url: "manager_listbyPart",
        data: {user_id:$("#username").val()},
        dataType: "json",
        success: function(data){
        			var partslists=[]; 
                    $('#right-parts').empty();   //清空right-parts里面的所有内容
                    var html = ''; 
                    $.each(eval(data), function(i,msg){
                    	$label = $("#right-parts").append("<label id=label"+i+">"+msg.part_name+"</label>");
                    	$("#label"+i).append(" <input type='checkbox' name='allparts' value="+msg.id+">");
                    	partslists.push(msg.id);
                    });
                    //定义局部变量（刚刚生成的checkbox）
                    
                    var $parts = $('input[name="allparts"]');
                    $part.each(function() {
                    		for(var i=0;i<$parts.length;i++){
                    		if ($(this).attr('value')==$parts[i].value) {
                        	$parts[i].checked='true';
                    		}
                    		}
                });
                 }
    });
}

$btn.click(function(){
	var chk_value =""; 
	$('input[name="allparts"]:checked').each(function(i,msg){ 
	chk_value+=$(this).val()+","; 
	});
	$.ajax({
        type: "post",
        url: "manager_userpart_update",
        data: {user_id:$("#userid").val(),part_id:chk_value},
        success: function(data){
        	if(data=='success'){
        	alert('success!');
        	window.location.reload();//刷新当前页面
        	zoo();
        	}else{
        	alert('failure!');	
        	}
        }
    });
});

var allselect = $("#allselet");
allselect.click(function(){
	var $parts = $('input[name="allparts"]');
    if(this.checked){
    	 $parts.each(function() {
    		 $(this).attr('checked','true');
    	 });
} else{
	 $parts.each(function() {
		 $(this).attr('checked',false);
	 });
 }
});
